<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class Index extends Base 
{
    public function index(){  
     $count['example'] = Db::name('example')->where(['userId'=>$this->userId])->count();
     $count['JoinQunConfig'] = Db::name('JoinQunConfig')->where(['userId'=>$this->userId])->count();
     $count['TgQun'] = Db::name('TgQun')->count();
     $count['GroupType'] = Db::name('GroupType')->count();
     
     $this->assign('count',$count);
     return $this->fetch();
    }

    public function relation_phone(){
        
     return $this->fetch('function/relation_phone');
    }
    
    public function join_qun_index(){
      //遗弃
      return $this->fetch('function/join_qun_index');  
    }
    
    public function join_qun(){
        
      //群数据
      $gt_id = input('get.gt_id');
      if(!$gt_id){
          $tg_qun = Db::name('TgQun')->select();
          foreach ($tg_qun as $key => &$value){
              //$name_cn = crawler_name($value['url']);
              $value['name_cn'] = isset($name_cn)?$name_cn:'error';
          }
      }else{
          $tg_qun = Db::name('TgQun')->where(['gt_id'=>$gt_id])->select();
          foreach ($tg_qun as $key => &$value){
              //$name_cn = crawler_name($value['url']);
              $value['name_cn'] = isset($name_cn)?$name_cn:'error';
          }
      }
      
      //group_type
      $group_type = Db::name('GroupType')->select();
      
      $example = Db::name('example')->where(['userId'=>$this->userId])->select();
      
      $join_qun_config = Db::name('JoinQunConfig')
      ->alias('a')
      ->join('example b','a.exampleId = b.id','LEFT')
      ->where(['a.userId'=>$this->userId])
      ->field('b.id as bid,a.id as aid,a.time as atime,a.*,b.*')
      ->order('aid desc')
      ->select();
      
      foreach ($join_qun_config as $key=>&$value){
          $keys = json_decode($value['data'],true);
          foreach ($keys as $k => &$v){
              $qunname = Db::name('TgQun')->where(['id'=>$v['qunId']])->find();
              $v['name'] = $qunname['name'];
          }
          $value['datas']  = $keys;
      }
      
      $empty = '<b style="padding-left:20px;">暂无配置</b>';
        
      return $this->fetch('function/join_qun',['tg_qun'=>$tg_qun,'group_type'=>$group_type,'examples'=>$example,'join_qun_config'=>$join_qun_config,'gtid'=>$gt_id?$gt_id:0,'empty'=>$empty]);
    }
    
    public function timing_advertising_qun(){
        
      //群数据
      $gt_id = input('get.gt_id');
      if(!$gt_id){
          $tg_qun = Db::name('TgQun')->select();
          foreach ($tg_qun as $key => &$value){
              //关闭后可提高加载速度
              //$name_cn = crawler_name($value['url']);
              $value['name_cn'] = isset($name_cn)?$name_cn:'error';
          }
      }else{
          $tg_qun = Db::name('TgQun')->where(['gt_id'=>$gt_id])->select();
          foreach ($tg_qun as $key => &$value){
              //关闭后可提高加载速度
              //$name_cn = crawler_name($value['url']);
              $value['name_cn'] = isset($name_cn)?$name_cn:'error';
          }
      }
      
      //group_type
      $group_type = Db::name('GroupType')->select();
      
      $example = Db::name('example')->where(['userId'=>$this->userId])->select();
      
      $timing_of_mass = Db::name('TimingOfMass')
      ->alias('a')
      ->join('example b','a.exampleId = b.id','LEFT')
      ->where(['a.userId'=>$this->userId])
      ->field('b.id as bid,a.id as aid,a.time as atime,a.*,b.*')
      ->order('aid desc')
      ->select();
      
      foreach ($timing_of_mass as $key=>&$value){
          $keys = json_decode($value['data'],true);
          foreach ($keys as $k => &$v){
              $qunname = Db::name('TgQun')->where(['id'=>$v['qunId']])->find();
              $v['name'] = $qunname['name'];
          }
          $value['datas']  = $keys;
      }
      
      $empty = '<b style="padding-left:20px;">暂无任务</b>';   
        
      return $this->fetch('function/timing_advertising_qun',['tg_qun'=>$tg_qun,'group_type'=>$group_type,'examples'=>$example,'timing_of_mass'=>$timing_of_mass,'gtid'=>$gt_id?$gt_id:0,'empty'=>$empty]);  
    }
    
    public function timing_advertising_si(){
      return $this->fetch('function/timing_advertising_si');  
    }
    
    public function share_qun(){
        
      $GroupType = Db::name('GroupType')->select();
      
      
      return $this->fetch('function/share_qun',['GroupType'=>$GroupType]);  
    }
    
    public function share_bot(){
      return $this->fetch('function/share_bot');  
    }
    
    public function aibot_list(){
       $example = Db::name('example')->where(['userId'=>$this->userId])->paginate(10);
       
       return $this->fetch('aibot/aibot_list',['example'=>$example]);  
    }
    
    public function add_ai_bot(){
        $name = input('get.name');
        if(!$name){
            exit(json_encode(['code'=>1,'msg'=>'请输入实例名']));
        }
        
        $WEBNAME = $_SERVER['SERVER_NAME'];
        return $this->fetch('aibot/add_ai_bot',['name'=>$name,'webname'=>$WEBNAME]);
    }
    
    public function look_contact($name,$username){
        
        
        return $this->fetch('aibot/look_contact',['name'=>$name]);
    }
    
    public function qun_member($name,$id){
        
        $list = $this->pmhapp->异步群成员_完整($name,'https://t.me/hhys1601',false);
        dump($list);
    }
     
    public function ceshi(){
        $sidebar = $this->sidebar();
        dump($sidebar);
    }
    
    
    public function sidebar(){
        return [
            array('level1'=>'基础配置','data'=>[['level2'=>'绑定账号','url'=>''],['level2'=>'自动加群','url'=>'']]),
            array('level1'=>'服务项目','data'=>[['level2'=>'定时发送广告','url'=>''],['level2'=>'发送私人广告','url'=>'']]),
        ];
        
        
    }
}
